<?php

$operation = $_GET["OP"];
$response = array("OP" => $operation, "success" => 0, "error" => 0);
$invalidOperation = false;

switch ($operation) {
    case "login":

        /*
          http://alertatics.comule.com/api.php?USR=numero_telefonico&HASH=password_Hasheado&PAIS=BO&TOKEN=E6CC90B878B948C35E92B003C792C46C58C4AF40&OP=login
         */
        $user = $_GET["USR"];
        $pais = $_GET["PAIS"];
        $hash = $_GET["HASH"];
        $token = $_GET["TOKEN"];

        if ($user == '' || $hash == '' || $pais == '' || $token == '') {
            echo 'faltan parametros';
            break;
        }
        $transformedToken = sha1("$user&$pais");
        if ($transformedToken != $token) {
            $response["success"] = 0;
            $response["error"] = 1;
            break;
        }
        require_once 'dal/controller/UsuarioController.php';
        $user = getUsuarioByLogin($user, $hash, $pais);
        $response["success"] = 1;
        $response["USRCODE"] = $user["token"];
    case "help":
        //Aqui deberia hacer la peticion de ayuda
        /**
         * http://alertatics.comule.com/api.php?USRCODE=codigo_usuario&TOKEN=token_guardado_anteriormente&OP=help
         */
        $userCode = $_GET["USRCODE"];

        require_once 'dal/controller/UsuarioController.php';
        $user = getUsuarioByToken($userCode);
        if ($user == null) {
            $response["success"] = 0;
            $response["error"] = 1;
            break;
        }

        $token = $_GET["TOKEN"];

        if ($transformedToken != $user["token"]) {
            $response["success"] = 0;
            $response["error"] = 1;
            break;
        }

        $response["success"] = 1;
        break;
    case "linkedUsers":
        //Aqui deberia obtenerse la lista de telefonos asociados al usuario
        /**
         * http://alertatics.comule.com/api.php?USRCODE=codigo_usuario&TOKEN=token_guardado_anteriormente&OP=linkedUsers
         */
        $userCode = $_GET["USRCODE"];

        require_once 'dal/controller/UsuarioController.php';
        $user = getUsuarioByToken($userCode);
        if ($user == null) {
            $response["success"] = 0;
            $response["error"] = 1;
            break;
        }

        $token = $_GET["TOKEN"];

        if ($transformedToken != $user["token"]) {
            $response["success"] = 0;
            $response["error"] = 1;
            break;
        }

        $res = "";

        require_once 'dal/controller/UsuarioLinkeadoController.php';

        $linkedUsersList = getLinkedUsers($user["idUsuario"]);

        foreach ($linkedUsersList as $linkedList) {
            $res.= ";" . $linkedUsersList["telefonoLinkeado"];
        }

        $response["success"] = 1;
        $response["linkedUsers"] = $res;

        break;
    default:
        $invalidOperation = true;
        $response["error"] = 1;
        break;
}
if ($invalidOperation) {
    echo "Access Denied";
} else {
    echo json_encode($response);
}
?>